#include "PacsClient.h"
#include <QAtomicPointer>
#include "SCommon.h"

PacsClient * PacsClient::g_pacsClient = NULL;
QMutex PacsClient::m_Mutex;
PacsClient::PacsClient()
{
	g_mainWidget = nullptr;
	g_currentPatient = nullptr;
//	m_dbOperator = DBOperate::GetKernel();
// 	g_mainWidget = new Pacs_MainWidget;
// 	g_mainWidget->hide();
	DBOperate::GetKernel()->DBconnect();
	g_nTodayPatientOrderNum = 0;
	g_nNowDateTime = ((QDateTime)QDate::currentDate()).toSecsSinceEpoch();

} 
const QString& PacsClient::setCurrentSicker(const QString& strSicker)
{ 
	// 	g_currentSicker = strSicker; 
	// 	return g_currentFilePath = SCommon::createCurrentSickDir(strSicker);
	return "";
}

PacsClient* PacsClient::getClientInstance()
{
	QMutexLocker mutexLocker(&m_Mutex);
	if(g_pacsClient == NULL)
	{
		g_pacsClient = new PacsClient();
	}
	return g_pacsClient;
}

void PacsClient::quitApp()
{
	g_mainWidget->hide();
	delete g_mainWidget;
	QApplication::quit();
}
Pacs_MainWidget* PacsClient::getMainWidget()
{
	if (g_mainWidget == NULL)
	{
		g_mainWidget = new Pacs_MainWidget;
	}
	return g_mainWidget;
}

void PacsClient::emitNewPatined(bool isCreate,const QString& strCheckNum)
{
	/*void createNewPatient(const QString& strCheckNum);
	void saveNewPatient(const QString& strCheckNum);*/
	if (isCreate)
	{
		emit createNewPatient(strCheckNum);
	} 
	else
	{
		emit saveNewPatient(strCheckNum);
	}
}


void PacsClient::setLoginName(const QString& userName)
{
	if (userName.trimmed().isEmpty())
	{
		m_strUserName = "admin";
	}
	else
	{
		m_strUserName = userName;
	}
	m_userPath = SCommon::createUserDir(m_strUserName);
}

void PacsClient::SetCurrentPatient(Pacs_Patient *nowPatient)
{
	 g_currentPatient = nowPatient;
}
void PacsClient::ClearCurrentPatient()
{
	if (g_currentPatient)
	{
		delete g_currentPatient;
	}
	g_currentPatient = nullptr;
}
Pacs_Patient* PacsClient::GetCurrentPatient()
{
	return g_currentPatient;
}

PacsClient::~PacsClient()
{
	delete g_mainWidget;
}
 